/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.personnel.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.admin.api.entity.SysUserinfo;
import com.fuyao.cloud.admin.api.feign.RemoteUserInfoService;
import com.fuyao.cloud.common.core.constant.SecurityConstants;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.excel.handler.CustomColEventListener;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.common.security.util.SecurityUtils;
import com.fuyao.cloud.personnel.api.entity.Salary;
import com.fuyao.cloud.personnel.service.ColumncontrolService;
import com.fuyao.cloud.personnel.service.SalaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工资记录表
 *
 * @author 碎月
 * @date 2021-12-16 10:35:16
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/salary" )
@Api(value = "salary", tags = "工资记录表管理")
public class SalaryController {

    private final SalaryService salaryService;
    private final RemoteUserInfoService remoteUserInfoService;
    private final ColumncontrolService columncontrolService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param salary 工资记录表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('personnel_salary_view')")
    public R getFyPersonnelSalaryPage(Page page, Salary salary, String scope) {
        if (SecurityUtils.getUser().getAuthorities().stream().anyMatch(grantedAuthority -> "personnel_salary_edit".equals(grantedAuthority.getAuthority())) && "all".equals(scope)) {
            salary.setUserNumber(null);
        } else {
            R<SysUserinfo> res = remoteUserInfoService.getUserInfoByUserName(SecurityConstants.FROM_IN, SecurityUtils.getUser().getUsername());
            if (res.getCode() == 0 && res.getData() != null) {
                salary.setUserNumber(res.getData().getPersonNumber());
            } else {
                salary.setUserNumber("-1");
            }
        }
        return R.ok(salaryService.page(page, Wrappers.query(salary)));
    }

    /**
     * 根据月份和年份分页查询
     * @param page 分页对象
     * @param year 年份
     * @param month 月份
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page/{year}/{month}")
    @PreAuthorize("@pms.hasPermission('personnel_salary_view')")
    public R getSalaryPage(Page page ,@PathVariable("year") String year ,@PathVariable("month") String month) {
        Salary salary =new Salary();
        salary.setYear(year);
        salary.setMonth(month);
        return R.ok(salaryService.page(page, Wrappers.query(salary)));
    }

    /**
     * 根据月份和年份分页查询
     * @param page 分页对象
     * @param year 年份
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page/{year}" )
    @PreAuthorize("@pms.hasPermission('personnel_salary_view')" )
    public R getSalaryPage(Page page ,@PathVariable("year") String year) {
        Salary salary =new Salary();
        salary.setYear(year);
        return R.ok(salaryService.page(page, Wrappers.query(salary)));
    }

    /**
     * 加载表头
     * @param year 年份
     * @return
     */
    @ApiOperation(value = "加载表头", notes = "加载表头")
    @GetMapping("/loadTableHead/{year}")
    public R loadTableHead(@PathVariable( name = "year",value = "") String year) {
        return R.ok(this.columncontrolService.loadTableHead(year));
    }


    /**
     * 通过id查询工资记录表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('personnel_salary_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(salaryService.getById(id));
    }

    /**
     * 新增工资记录表
     * @param salary 工资记录表
     * @return R
     */
    @ApiOperation(value = "新增工资记录表", notes = "新增工资记录表")
    @SysLog("新增工资记录表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('personnel_salary_add')" )
    public R save(@RequestBody Salary salary) {
        return R.ok(salaryService.save(salary));
    }

    /**
     * 修改工资记录表
     * @param salary 工资记录表
     * @return R
     */
    @ApiOperation(value = "修改工资记录表", notes = "修改工资记录表")
    @SysLog("修改工资记录表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('personnel_salary_edit')" )
    public R updateById(@RequestBody Salary salary) {
        return R.ok(salaryService.updateById(salary));
    }

    /**
     * 通过id删除工资记录表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除工资记录表", notes = "通过id删除工资记录表")
    @SysLog("通过id删除工资记录表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('personnel_salary_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(salaryService.removeById(id));
    }

    /**
     * 移除多条
     * @param ids id
     * @return R
     */
    @ApiOperation(value = "通过id删除工资记录表", notes = "通过id删除工资记录表")
    @SysLog("通过id删除工资记录表" )
    @PostMapping("/removeListByIds")
    @PreAuthorize("@pms.hasPermission('personnel_salary_del')" )
    public R removeByIds(@RequestBody List<Long> ids) {
        return R.ok(this.salaryService.removeByIds(ids));
    }

    /**
     * 删除所有
     * @return R
     */
    @ApiOperation(value = "删除所有", notes = "删除所有")
    @SysLog("删除所有" )
    @GetMapping("/removeAll")
    @PreAuthorize("@pms.hasPermission('personnel_salary_del')" )
    public R removeAll() {
        return R.ok(this.salaryService.remove(Wrappers.query()));
    }

    /**
     * 导出excel 表格
     * @param salary 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @PostMapping("/export")
    @PreAuthorize("@pms.hasPermission('personnel_salary_export')" )
    public List<Salary> export(Salary salary) {
        return salaryService.list(Wrappers.query(salary));
    }

    /**
     * 导入excel 表格
     * @param excelList 数据
     * @param bindingResult 错误信息
     * @return excel 文件流
     */
    @PostMapping("/import/{yearMonth}")
    @PreAuthorize("@pms.hasPermission('personnel_salary_import')" )
    public R importData(@RequestExcel(readListener = CustomColEventListener.class) List<Map<Integer,String>> excelList,
                        BindingResult bindingResult,
                        @PathVariable("yearMonth") String yearMonth) {
        return salaryService.importSalary(excelList, bindingResult, yearMonth);
    }

    /**
     * 根据年和月查找数据之后，群发
     * @param year 年份
     * @param month 月份
     * @return R
     */
    @ApiOperation(value = "通过id删除工资记录表", notes = "通过id删除工资记录表")
    @SysLog("通过id删除工资记录表" )
    @GetMapping("/sendMassSms/{year}/{month}")
    //@PreAuthorize("@pms.hasPermission('')" )
    public R sendMassSms(@PathVariable("year") Integer year,@PathVariable("month") Integer month) {
        return salaryService.sendMassSms(year,month);
    }

    /**
     * 根据年和月查找数据之后，群发
     * @param salaryId 工资记录id
     * @return R
     */
    @ApiOperation(value = "通过id删除工资记录表", notes = "通过id删除工资记录表")
    @SysLog("通过id删除工资记录表" )
    @PostMapping("/sendSelectMassSms")
    public R sendSelectMassSms(@RequestBody List<Long> salaryId) {
        return salaryService.sendSalaryIdSms(salaryId);
    }

    /**
     * 发送单条短信
     * @param userNumber 用户编号
     * @param year 年份
     * @param month 月份
     * @return R
     */
    @SysLog("发送短信" )
    @GetMapping("/sendSms/{userNumber}/{year}/{month}")
    //@PreAuthorize("@pms.hasPermission('')" )
    public R sendSms(@PathVariable("userNumber") String userNumber,
                     @PathVariable("year") Integer year,
                     @PathVariable("month") Integer month) {
        return salaryService.sendOneSms(userNumber,year,month);
    }


    @GetMapping("/yearMonthTree")
    public R getTree() {
        return R.ok(salaryService.selectTree());
    }

}
